package com.example.dell.library.activity;

import android.accounts.Account;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.dell.library.R;
import com.example.dell.library.bean.Book;
import com.example.dell.library.bean.BorrorBook;
import com.example.dell.library.bean.StaticStuBBook;
import com.example.dell.library.bean.Student;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class BookInforActivity extends AppCompatActivity {
    @BindView(R.id.boimg)
    ImageView boimg;
    @BindView(R.id.bookName)
    TextView bookName;
    @BindView(R.id.bookAuthor)
    TextView bookAuthor;
    @BindView(R.id.bookPublish)
    TextView bookPublish;
    @BindView(R.id.bookType)
    TextView bookType;
    @BindView(R.id.bookNo)
    TextView bookNo;
    @BindView(R.id.bookIsInit)
    TextView bookIsInit;
    @BindView(R.id.bookIsBor)
    Button bookIsBor;//借书按钮


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_infor);
        ButterKnife.bind(this);
        querry();
    }

    private void querry() {
        final Book book = getBookByLogin();//获取当前书籍信息**
        BmobQuery<BorrorBook> bmobQuery = new BmobQuery<BorrorBook>();
        bmobQuery.addWhereEqualTo("SID", StaticStuBBook.bid);
        bmobQuery.findObjects(new FindListener<BorrorBook>() {
            @Override
            public void done(List<BorrorBook> list, BmobException e) {
                if (e == null) {
                    Log.e("book", book.getName());
                    String bookname = book.getName();
                    for (int i = 0; i < list.size(); i++) {
                        final BorrorBook b = list.get(i);
                        JudgeBookNums(b, book);
                    }
                } else {
                    Log.e("err", e.toString());
                }
            }
        });
    }

    private void JudgeBookNums(final BorrorBook b, final Book book) {

        if (book.getBookNums() < 1) {
            bookIsBor.setText("库存不足");
            bookIsInit.setText("不在馆");
        } else if (book.getName().equals(b.getSBook1()) || book.getName().equals(b.getSBook2()) || book.getName().equals(b.getSBook3())) {
            bookIsBor.setText("已借");
        }
        else if (null != b.getSBook1() && null != b.getSBook2() && null != b.getSBook3()) {
            bookIsBor.setText("可借书籍已满3本");
            Log.e("·····", "++++++++++++++");
        }
        else {
            bookIsInit.setText("在馆");
            bookIsBor.setText("借");
            bookIsBor.setBackgroundColor(Color.argb(225, 66, 204, 00));
            bookIsBor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateBook(book);
                    updateStu(b, book);
                }
            });
        }
    }

    private void updateStu(BorrorBook sv, Book bk) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        BorrorBook p3 = new BorrorBook();
        if (sv.getSBook1() == null) {
            p3.setSBook1(bk.getName());
            p3.setTime1(simpleDateFormat.format(date));
        } else if (sv.getSBook2() == null) {
            p3.setSBook2(bk.getName());
            p3.setTime2(simpleDateFormat.format(date));
        } else if (sv.getSBook3() == null) {
            p3.setSBook3(bk.getName());
            p3.setTime3(simpleDateFormat.format(date));
        }
        StaticStuBBook.BBookid =sv.getObjectId();
        p3.update(sv.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    finish();
                } else {
                    Log.e("err", e.toString());
                }
            }
        });
    }

    private void updateBook(Book book) {
        final int n = book.getBookNums() - 1;
        Book p2 = new Book();
        p2.setBookNums(n);
        p2.update(book.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(BookInforActivity.this, "借书成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Log.e("err", e.toString());
                    Toast.makeText(BookInforActivity.this, "借书失败，请找客服", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @NonNull
    private Book getBookByLogin() {//获取当前书籍信息**
        String BookJson = getIntent().getStringExtra("BOOK");
        final Book book = new Gson().fromJson(BookJson, Book.class);
        Glide.with(this).load(Uri.parse(book.getImgFile().getFileUrl())).into(boimg);
        bookNo.setText(String.valueOf(book.getBookNums()));
        bookName.setText(book.getName());
        bookAuthor.setText(book.getAuthor());
        bookPublish.setText(book.getPublish());
        bookType.setText(book.getType());
        return book;
    }
}
